package com.ufgov.agcfs.baseData.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ufgov.agcfs.baseData.dao.BookTypeDao;
import com.ufgov.agcfs.baseData.dao.CritAccDao;
import com.ufgov.agcfs.baseData.entity.BookType;
import com.ufgov.agcfs.baseData.entity.CritAcc;
import com.ufgov.agcfs.pub.UUID;

@Service
public class BookTypeService {
	@Autowired
	public BookTypeDao bookTypeDao;
	
	@Autowired
	public CritAccDao critAccDao;
	
	public List<BookType> findAll(){
		BookType bookType = new BookType();
		return bookTypeDao.findAllBookType(bookType);
	}
	
	public List<BookType> findByNd(String nd){
		BookType bookType = new BookType();
		bookType.setNd(nd);
		return bookTypeDao.findAllBookType(bookType);
	}
	
	public BookType findByNdCode(String nd,String bookTypeCode){
		BookType bookType = new BookType();
		bookType.setNd(nd);
		bookType.setAccBookTypeCode(bookTypeCode);
		List<BookType> bookList = bookTypeDao.findAllBookType(bookType);
		if (bookList.size()==0){
			return null;
		} else {
			return (BookType)bookList.get(0);
		}
	}
	
	public BookType findById(String id){
		BookType bookType = new BookType();
		bookType.setId(id);
		List<BookType> bookList = bookTypeDao.findAllBookType(bookType);
		if (bookList.size()==0){
			return null;
		} else {
			return (BookType)bookList.get(0);
		}
	}
	
	public List<String> getAllNd(){
//		return bookTypeDao.getAllNd();
		Date nowTime=new Date(); 
		SimpleDateFormat dataFmt=new SimpleDateFormat("yyyy"); 
		String curYear = dataFmt.format(nowTime); 
		List<String> ndList = new ArrayList<String>();
		for (int i = 0; i < 5; i++) {
			ndList.add(String.valueOf(Integer.parseInt(curYear)+i));
		}
		return ndList;
	}
	
	@Transactional
	public void deleteBookType(JSONArray jsonArray,Map<String, String> resultMap){
		for (Object obj : jsonArray) {
			//用json生成实体类型
			JSONObject jsonObj = (JSONObject)obj;
			BookType bookType = (BookType) JSONObject.toBean(jsonObj,BookType.class);
			//
			BookType book = findById(bookType.getId());
			if (book!=null){
				if (book.equals(bookType)){
					CritAcc critAcc = new CritAcc();
					critAcc.setNd(bookType.getNd());
					critAcc.setAccBookTypeCode(bookType.getAccBookTypeCode());
					if (critAccDao.searchCritAcc(critAcc).size()>0){
						getErrorMsg(resultMap,"saveError","科目信息已在标准会计科目中使用，不可删除！");
						return;
					}else
						bookTypeDao.deleteBookType(bookType);
				} else {
					getErrorMsg(resultMap,"saveError","科目信息与系统中不一致！");
					return;
				}
			}
		}
	}
	
	@Transactional
	public void saveBookType(BookType bookType,Map<String, String> resultMap){
		if ((bookType.getId()==null)||(bookType.getId().equals(""))) {
			BookType book = findByNdCode(bookType.getNd(),bookType.getAccBookTypeCode());
			if (book!=null){
				getErrorMsg(resultMap,"saveError","系统中已存在相同科目！");
				return;
			}
			bookType.setId(UUID.randomUUID().toString());
			bookTypeDao.insertBookType(bookType);
		} else {
			BookType book = findById(bookType.getId());
			if (book.equals(bookType)){
				bookTypeDao.updateBookType(bookType);
			} else {
				getErrorMsg(resultMap,"saveError","科目信息与系统中不一致！");
			}
		}	
	}
	
	
	private void getErrorMsg(Map<String, String> resultMap,String flg, String reason) {
		resultMap.put("result", "fail");
		resultMap.put(flg, reason);
		resultMap.put("reason", "保存失败！");
	}
}
